/*     */ package com.zimbra.cs.account.accesscontrol;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.GuestAccount;
/*     */ import com.zimbra.cs.account.MailTarget;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class PermissionCache
/*     */ {
/*  32 */   private static boolean cacheEnabled = LC.acl_cache_enabled.booleanValue();
/*     */   
/*     */   static enum CachedPermission {
/*  35 */     NOT_CACHED(null, (short)0), 
/*  36 */     NO_MATCHING_ACL(null, (short)0), 
/*  37 */     ALLOWED(Boolean.TRUE, (short)1), 
/*  38 */     DENIED(Boolean.FALSE, (short)2);
/*     */     
/*     */     private final Boolean result;
/*     */     private final short cacheMask;
/*     */     
/*     */     private CachedPermission(Boolean result, short cacheMask) {
/*  44 */       this.result = result;
/*  45 */       this.cacheMask = cacheMask;
/*     */     }
/*     */     
/*     */     Boolean getResult() {
/*  49 */       assert (this != NOT_CACHED);
/*  50 */       return this.result;
/*     */     }
/*     */     
/*     */     short getCacheMask() {
/*  54 */       return this.cacheMask;
/*     */     }
/*     */   }
/*     */   
/*     */   public static void invalidateCache() {
/*  59 */     RightBearer.Grantee.clearGranteeCache();
/*  60 */     RightCommand.clearAllEffectiveRightsCache();
/*  61 */     PermCacheManager.getInstance().invalidateCache();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void invalidateAllCache()
/*     */   {
/*  76 */     Provisioning prov = Provisioning.getInstance();
/*     */     try {
/*  78 */       prov.flushCache(CacheEntryType.all, null);
/*     */     } catch (ServiceException e) {
/*  80 */       ZimbraLog.acl.warn("unable to flush cache", e);
/*     */     }
/*     */     
/*     */ 
/*  84 */     invalidateCache();
/*     */   }
/*     */   
/*     */   public static void invalidateCache(Entry target) {
/*  88 */     PermCacheManager.getInstance().invalidateCache(target);
/*     */   }
/*     */   
/*     */   public static double getHitRate() {
/*  92 */     return PermCacheManager.getInstance().getHitRate();
/*     */   }
/*     */   
/*     */   static CachedPermission cacheGet(MailTarget grantee, Entry target, Right rightNeeded, boolean canDelegateNeeded) {
/*  96 */     if (!cacheEnabled) {
/*  97 */       return CachedPermission.NOT_CACHED;
/*     */     }
/*     */     
/* 100 */     String cacheKey = buildCacheKey(grantee, rightNeeded, canDelegateNeeded);
/* 101 */     if (cacheKey == null)
/*     */     {
/* 103 */       return CachedPermission.NOT_CACHED;
/*     */     }
/*     */     
/* 106 */     CachedPermission perm = PermCacheManager.getInstance().get(target, cacheKey, rightNeeded);
/*     */     
/* 108 */     if (ZimbraLog.acl.isDebugEnabled()) {
/* 109 */       ZimbraLog.acl.debug("PermissionCache get: " + perm.toString() + " (target=" + target.getLabel() + ", grantee=" + grantee.getName() + ", right=" + rightNeeded.getName() + ", canDelegateNeeded=" + canDelegateNeeded + ")");
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 114 */     return perm;
/*     */   }
/*     */   
/*     */ 
/*     */   static void cachePut(MailTarget grantee, Entry target, Right rightNeeded, boolean canDelegateNeeded, Boolean allowed)
/*     */   {
/* 120 */     if (!cacheEnabled) {
/* 121 */       return;
/*     */     }
/*     */     
/* 124 */     String cacheKey = buildCacheKey(grantee, rightNeeded, canDelegateNeeded);
/* 125 */     if (cacheKey == null) {
/* 126 */       return;
/*     */     }
/*     */     
/* 129 */     CachedPermission perm = allowed.booleanValue() ? CachedPermission.ALLOWED : allowed == null ? CachedPermission.NO_MATCHING_ACL : CachedPermission.DENIED;
/*     */     
/*     */ 
/* 132 */     PermCacheManager.getInstance().put(target, cacheKey, rightNeeded, perm);
/*     */     
/* 134 */     if (ZimbraLog.acl.isDebugEnabled()) {
/* 135 */       ZimbraLog.acl.debug("PermissionCache put: " + perm.toString() + " (target=" + target.getLabel() + ", grantee=" + grantee.getName() + ", right=" + rightNeeded.getName() + ", canDelegateNeeded=" + canDelegateNeeded + ")");
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static String buildCacheKey(MailTarget grantee, Right rightNeeded, boolean canDelegateNeeded)
/*     */   {
/* 171 */     if (!rightNeeded.isCacheable()) {
/* 172 */       return null;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 178 */     if ((!rightNeeded.isUserRight()) && (Rights.Admin.R_adminLoginAs != rightNeeded)) {
/* 179 */       return null;
/*     */     }
/* 181 */     String id = null;
/* 182 */     if ((grantee instanceof GuestAccount))
/*     */     {
/*     */ 
/*     */ 
/* 186 */       id = ((GuestAccount)grantee).getDigest();
/* 187 */       if (id != null) {
/* 188 */         id = id + "G";
/*     */       } else {
/* 190 */         id = ((GuestAccount)grantee).getAccessKey();
/* 191 */         if (id != null) {
/* 192 */           id = id + "K";
/*     */         }
/*     */       }
/*     */     } else {
/* 196 */       id = grantee.getId();
/*     */     }
/*     */     
/* 199 */     if (id == null)
/*     */     {
/* 201 */       ZimbraLog.acl.debug("unable to build cache key: " + grantee.getName());
/* 202 */       return null;
/*     */     }
/*     */     char adminFlag;
/*     */     char adminFlag;
/* 206 */     if ((grantee instanceof Account)) {
/* 207 */       Account granteeAcct = (Account)grantee;
/* 208 */       adminFlag = granteeAcct.isIsDelegatedAdminAccount() ? '1' : granteeAcct.isIsAdminAccount() ? '2' : '0';
/*     */     } else {
/* 210 */       adminFlag = '0';
/*     */     }
/* 212 */     char canDelegate = canDelegateNeeded ? '1' : '0';
/* 213 */     return id + adminFlag + canDelegate;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/accesscontrol/PermissionCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */